<template>
  <div class="app-container">
    <h2>用户申请借款中心</h2>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="loanUserId">
        <el-input
          v-model="queryParams.loanUserId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款人名称" prop="loanLoanUserName">
        <el-input
          v-model="queryParams.loanLoanUserName"
          placeholder="请输入借款人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款人身份证" prop="loanLoanUserPhone">
        <el-input
          v-model="queryParams.loanLoanUserPhone"
          placeholder="请输入借款人身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="贷款产品id" prop="loanLoanProductsId">
        <el-input
          v-model="queryParams.loanLoanProductsId"
          placeholder="请输入贷款产品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户贷款金额" prop="loanLoanMoney">
        <el-input
          v-model="queryParams.loanLoanMoney"
          placeholder="请输入用户贷款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户贷款时间" prop="loanLoanLastTerm">
        <el-input
          v-model="queryParams.loanLoanLastTerm"
          placeholder="请输入用户贷款时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户实际支付金额" prop="loanActualMoney">
        <el-input
          v-model="queryParams.loanActualMoney"
          placeholder="请输入用户实际支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="loanLoanCard">
        <el-input
          v-model="queryParams.loanLoanCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款签章id" prop="loanSignatureId">
        <el-input
          v-model="queryParams.loanSignatureId"
          placeholder="请输入借款签章id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:loan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:loan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:loan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:loan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="loanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="借款id" align="center" prop="loanId" />
      <el-table-column label="用户id" align="center" prop="loanUserId" />
      <el-table-column label="借款人名称" align="center" prop="loanLoanUserName" />
      <el-table-column label="借款人手机号" align="center" prop="loanLoanUserPhone" />
      <el-table-column label="身份证" align="center" prop="loanLoanCard" >
        <template slot-scope="scope">
            <el-avatar shape="square" :size="50" :src="scope.row.loanLoanCard"></el-avatar>
          </template>
        </el-table-column>
      <el-table-column label="贷款产品id" align="center" prop="loanLoanProductsId" />
      <el-table-column label="用户贷款金额" align="center" prop="loanLoanMoney" />
      <el-table-column label="用户贷款时间" align="center" prop="loanLoanLastTerm" />
      <el-table-column label="用户实际支付金额" align="center" prop="loanActualMoney" />
      <el-table-column label="借款签章id" align="center" prop="loanSignatureId" />
      <el-table-column label="利息状态：0:未审核，1:审核通过，2:审核驳回，3:签章成功，4:签章失败" align="center" prop="loanStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:loan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:loan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改借款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="loanUserId">
          <el-input v-model="form.loanUserId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="借款人姓名" prop="loanLoanUserName">
          <el-input v-model="form.loanLoanUserName" placeholder="请输入借款人姓名" />
        </el-form-item>
        <el-form-item label="借款人手机号" prop="loanLoanUserPhone">
          <el-input v-model="form.loanLoanUserPhone" placeholder="请输入借款人手机号" />
        </el-form-item>
        <el-form-item label="贷款产品id" prop="loanLoanProductsId">
          <el-input v-model="form.loanLoanProductsId" placeholder="请输入贷款产品id" />
        </el-form-item>
        <el-form-item label="用户贷款金额" prop="loanLoanMoney">
          <el-input v-model="form.loanLoanMoney" placeholder="请输入用户贷款金额" />
        </el-form-item>
        <el-form-item label="用户贷款时间" prop="loanLoanLastTerm">
          <el-input v-model="form.loanLoanLastTerm" placeholder="请输入用户贷款时间" />
        </el-form-item>
        <el-form-item label="用户实际支付金额" prop="loanActualMoney">
          <el-input v-model="form.loanActualMoney" placeholder="请输入用户实际支付金额" />
        </el-form-item>
        <el-form-item label="身份证" prop="loanLoanCard">
          <el-input v-model="form.loanLoanCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="借款签章id" prop="loanSignatureId">
          <el-input v-model="form.loanSignatureId" placeholder="请输入借款签章id" />
        </el-form-item>
        <el-form-item label="删除状态：0:未删除,2:已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态：0:未删除,2:已删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLoan, getLoan, delLoan, addLoan, updateLoan } from "@/api/loan/loanlist";

export default {
  name: "Loan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借款表格数据
      loanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        loanUserId: null,
        loanLoanUserName: null,
        loanLoanUserPhone: null,
        loanLoanProductsId: null,
        loanLoanMoney: null,
        loanLoanLastTerm: null,
        loanActualMoney: null,
        loanLoanCard: null,
        loanSignatureId: null,
        loanStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        loanUserId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        loanLoanUserName: [
          { required: true, message: "借款人名称不能为空", trigger: "blur" }
        ],
        loanLoanUserPhone: [
          { required: true, message: "借款人手机号不能为空", trigger: "blur" }
        ],
        loanLoanProductsId: [
          { required: true, message: "贷款产品id不能为空", trigger: "blur" }
        ],
        loanLoanMoney: [
          { required: true, message: "用户贷款金额不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询借款列表 */
    getList() {
      this.loading = true;
      listLoan(this.queryParams).then(response => {
        this.loanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        loanId: null,
        loanUserId: null,
        loanLoanUserName: null,
        loanLoanUserPhone: null,
        loanLoanProductsId: null,
        loanLoanMoney: null,
        loanLoanLastTerm: null,
        loanActualMoney: null,
        loanLoanCard: null,
        loanSignatureId: null,
        loanStatus: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.loanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加借款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const loanId = row.loanId || this.ids
      getLoan(loanId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改借款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.loanId != null) {
            updateLoan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLoan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const loanIds = row.loanId || this.ids;
      this.$modal.confirm('是否确认删除借款编号为"' + loanIds + '"的数据项？').then(function() {
        return delLoan(loanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/loan/export', {
        ...this.queryParams
      }, `loan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
